package com.walkline.ui;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.component.LabelField;
    
public class ListStyleButtonField extends Field
{
    public static int DRAWPOSITION_TOP = 0;
    public static int DRAWPOSITION_BOTTOM = 1;
    public static int DRAWPOSITION_MIDDLE = 2;
    public static int DRAWPOSITION_SINGLE = 3;
    
    private static final int CORNER_RADIUS = 16;
    
    private static final int HPADDING = Display.getWidth() <= 320 ? 6 : 8;
    private static final int VPADDING = 4;
    
    private static final int COLOR_BACKGROUND = 0xFFFFFF;
    private static final int COLOR_BORDER = 0xBBBBBB;
    private static final int COLOR_BACKGROUND_FOCUS = 0x186DEF;
    private static final int COLOR_BACKGROUND_TITLE = 0xA9A9A9;

    private MyLabelField _labelField;
    private MyLabelField _labelFieldRight;
    private Bitmap _leftIcon;
    private Bitmap _actionIcon;
    
    private int _targetHeight;
    private int _rightOffset;
    private int _leftOffset;
    private int _labelHeight;
    
    private int _drawPosition = -1;

    public ListStyleButtonField(String label, long style)
    {
        this(null, label, null,	 null, style );
    }
    
    public ListStyleButtonField(String label, String labelright, long style)
    {
    	this(null, label, labelright, null, style);
	}
   
    //public ListStyleButtonField( String label, Bitmap actionIcon )
    //{
    //   this( label, actionIcon, 0 );
    //}
    
    //public ListStyleButtonField( String label, Bitmap actionIcon, long style )
    //{
    //	this( null, label,null, actionIcon, style );
    //}
    
    //public ListStyleButtonField( Bitmap icon, String label, long style )
    //{
    //    this( icon, label,null, null, style );
    //}
    
    public ListStyleButtonField(String label, String labelright, Bitmap actionIcon)
    {
    	this(null, label, labelright, actionIcon, 0);
    }

    public ListStyleButtonField( Bitmap icon, String label, String labelright, Bitmap actionIcon, long style )
    {
        super( USE_ALL_WIDTH | Field.FOCUSABLE );
               
        _labelField = new MyLabelField( label, style);
        if(labelright!=null) {_labelFieldRight=new MyLabelField(labelright, style);}
        _actionIcon = actionIcon;
        _leftIcon = icon;
    }
    
    public void setDrawPosition( int drawPosition )
    {
        _drawPosition = drawPosition;
    }
    
    public String toString()
    {
        return _labelField.toString();
    }
    
    public String toRightString()
    {
    	return _labelFieldRight.toString();
    }
    
    public void layout( int width, int height )
    {

        _targetHeight = getFont().getHeight() / 2 * 3 + 2 * VPADDING;

        if( Touchscreen.isSupported() ) {
            _targetHeight = getFont().getHeight() * 2 + 2 * VPADDING;
        }
    	
        _leftOffset = HPADDING;
        if( _leftIcon != null ) {
            _leftOffset += _leftIcon.getWidth() + HPADDING;
        }
        
        _rightOffset = HPADDING;
        if( _actionIcon != null ) {
            _rightOffset += _actionIcon.getWidth() + HPADDING;
        }
        
        _labelField.layout( width - _leftOffset - _rightOffset, height);        
        _labelHeight = _labelField.getHeight();
        int labelWidth = _labelField.getWidth();

    	if(_labelField.isStyle( DrawStyle.HCENTER))
    	{
            _leftOffset = ( width - labelWidth ) / 2;
        } else if ( _labelField.isStyle( DrawStyle.RIGHT ) ) {
            _leftOffset = width - labelWidth - HPADDING - _rightOffset;
        }
        
        if(_labelFieldRight!=null)
        {
        	_labelFieldRight.layout(width- _leftOffset - _rightOffset, height);	
        }
        
        int extraVPaddingNeeded = 0;
        if( _labelHeight < _targetHeight ) {
            // Make sure that they are at least 1.5 times font height
            extraVPaddingNeeded =  ( _targetHeight - _labelHeight ) / 2;
        }
        
        if(_labelFieldRight!=null)
        {
        	setExtent(width, _labelHeight + extraVPaddingNeeded);
        } else {
        	setExtent(width, _labelHeight +  extraVPaddingNeeded);
        }
    }

    public void setText( String text )
    {
        _labelField.setText( text );
        updateLayout();
    }
    
    public void setRightText(String text)
    {
    	_labelFieldRight.setText(text);
    	updateLayout();
	}
    
    protected void paint( Graphics g )
    {
        // Left Bitmap
        if( _leftIcon != null ) {
            g.drawBitmap( HPADDING, VPADDING, _leftIcon.getWidth(), _leftIcon.getHeight(), _leftIcon, 0, 0 );
        }
        
        // Text
        if(_labelField!=null)
        {
            try 
            {
                g.pushRegion( _leftOffset, (getHeight() - _labelHeight) / 2, getWidth() - _leftOffset - _rightOffset, _labelHeight, 0, 0 );
                _labelField.paint( g );
            } finally {
                g.popContext();
            }
        }
        
        // Right (Action) Bitmap
        if( _actionIcon != null ) {
            g.drawBitmap( getWidth() - HPADDING - _actionIcon.getWidth(), ( getHeight() - _actionIcon.getHeight() ) / 2, _actionIcon.getWidth(), _actionIcon.getHeight(), _actionIcon, 0, 0 );
        }
        
        //Text Right
        if(_labelFieldRight !=null){
        	try {
        		g.pushRegion(getWidth() - HPADDING * 2 - _labelFieldRight.getWidth() - (_actionIcon != null ? _actionIcon.getWidth() : 0),(getHeight() - _labelFieldRight.getHeight())/2,_labelFieldRight.getWidth(), _labelFieldRight.getHeight(),0,0);
        		_labelFieldRight.paint(g);
        	} finally{
        		g.popContext();
        	}
        }
    }
    
    protected void paintBackground( Graphics g )
    {
        if( _drawPosition < 0 ) {
            // it's like a list field, let the default background be drawn
            super.paintBackground( g );
            return;
        }
        
        int oldColour = g.getColor();
        
        //int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? COLOR_BACKGROUND_FOCUS : COLOR_BACKGROUND;
        int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? COLOR_BACKGROUND_FOCUS : COLOR_BACKGROUND;
        if(background==COLOR_BACKGROUND && _labelFieldRight==null) {background=COLOR_BACKGROUND_TITLE;}
        
        try {
            if( _drawPosition == 0 ) {
                // Top
                g.setColor( background );
                g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
                g.setColor( COLOR_BORDER );
                g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
                g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
            } else if( _drawPosition == 1 ) {
                // Bottom 
                g.setColor( background );
                g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
                g.setColor( COLOR_BORDER );
                g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
            } else if( _drawPosition == 2 ) {
                // Middle
                g.setColor( background );
                g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
                g.setColor( COLOR_BORDER );
                g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
                g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
            } else {
                // Single
                g.setColor( background );
                g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
                g.setColor( COLOR_BORDER );
                g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
            }
        } finally {
            g.setColor( oldColour );
        }
    }
    
    protected void drawFocus( Graphics g, boolean on )
    {
        if( _drawPosition < 0 ) {
            super.drawFocus( g, on );
        } else {
            boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
            try {
                if( on ) {
                	g.setColor(Color.WHITE);
                    g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
                }
                paintBackground( g );
                paint( g );
            } finally {
                g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
            }
        }
    }
    
    protected boolean keyChar( char character, int status, int time ) 
    {
    	switch (character)
    	{
			case Characters.ENTER:
	            clickButton();
	            return true;
        }

        return super.keyChar(character, status, time);
    }
    
    protected boolean navigationUnclick(int status, int time) 
    {
    	if ((status & KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY)
    	{
    		clickButton(); 
    		return true;
    	}

    	return super.navigationClick(status, time);
    }

    protected boolean trackwheelClick(int status, int time)
    {
    	if ((status & KeypadListener.STATUS_TRACKWHEEL) == KeypadListener.STATUS_TRACKWHEEL)
    	{      
    		clickButton();    
    		return true;
    	}

    	return super.trackwheelClick(status, time);
    }

    protected boolean invokeAction(int action) 
    {
        switch(action)
        {
            case ACTION_INVOKE:
                clickButton(); 
                return true;
        }

        return super.invokeAction(action);
    }
         
    protected boolean touchEvent(TouchEvent message)
    {
        int x = message.getX( 1 );
        int y = message.getY( 1 );

        if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {return false;}
        
        switch (message.getEvent())
        {
            case TouchEvent.UNCLICK:
            	clickButton();
            	return true;
        }

        return super.touchEvent(message);
    }

    public void clickButton() {fieldChangeNotify(0);}

    public void setDirty( boolean dirty ) {}
    public void setMuddy( boolean muddy ) {}
    
    private static class MyLabelField extends LabelField
    {
        
        public MyLabelField( String text, long style )
        {
            super( text, style );
        }
    
        public void layout( int width, int height )
        {   
            super.layout( width, height );
        }   
        
        public void paint( Graphics g ) 
        {
            super.paint( g );
        }
    }
}